package com.bulletelc.apollopre.module.display.product.details.comment;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.CommentBean;
import com.bulletelc.apollopre.framework.MVPBaseFragment;
import com.bulletelc.apollopre.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/15
 * desc  : 商品详情的评论fragment
 */

public class CommentFragment extends MVPBaseFragment<CommentContract.View, CommentPresenter> implements CommentContract.View {

    private ProgressBar pbLoad;
    private TextView tvAmount, tvNoComments;
    private ListView listView;

    private List<CommentBean> commentsList;
    private CommentsListAdapter adapter;
    private boolean first = true;

    @Override
    public int getLayoutRes() {
        return R.layout.comment_list_fragment;
    }

    @Override
    public void assignViews() {
        pbLoad = findView(R.id.pbLoad);
        tvAmount = findView(R.id.tv_amount);
        listView = findView(R.id.lv_comments);
        tvNoComments = findView(R.id.tv_no_comments);
    }

    @Override
    public void setListener() {
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        commentsList = new ArrayList<>();
        adapter = new CommentsListAdapter(getActivity(), commentsList);
        listView.setAdapter(adapter);
    }

    @Override
    protected CommentPresenter createPresenter() {
        return new CommentPresenter();
    }

    @Override
    public void onSuccessCommentsList(List<CommentBean> comments) {
        if (comments != null && comments.size() > 0) {
            tvNoComments.setVisibility(View.GONE);
            commentsList.clear();
            commentsList.addAll(comments);
            tvAmount.setText("评价("+ comments.size() + ")");
            adapter.notifyDataSetChanged();
            listView.setVisibility(View.VISIBLE);
            tvNoComments.setVisibility(View.GONE);
        } else {
            tvAmount.setText("评价(0)");
            tvNoComments.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
        MyUtil.setProgressBar(pbLoad, false);

    }

    @Override
    public void onErrorCommentsList(String error) {
        Toast.makeText(baseActivity, R.string.network_msg, Toast.LENGTH_SHORT).show();
        MyUtil.setProgressBar(pbLoad, false);
    }

    @Override
    public void onFailCommentsList(String fail) {
        Toast.makeText(getContext(), fail, Toast.LENGTH_SHORT).show();
        MyUtil.setProgressBar(pbLoad, false);
    }

    /**
     * 更新数据
     */
    public void update(String id) {
        if (first) {
            presenter.getCommentsList(id);
            first = false;
            MyUtil.setProgressBar(pbLoad, false);
        }
    }

}
